package 哈夫曼编码;

import java.util.Arrays;
import java.util.Scanner;

public class 霍夫曼编码2 {

	public static void main(String[] args) throws InterruptedException {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			String s = sc.next();
			char[] chars = s.toCharArray();
			Arrays.sort(chars);
			int[][] arr = new int[2 * chars.length][2];
			int index = 0;
			for (int i = 0; i < chars.length;) {
				for (int j = i + 1; j <= chars.length; j++) {
					if (j == chars.length || chars[j] != chars[i]) {
						arr[index++][0] = j - i;
						i = j;
						break;
					}
				}
			}
			int line = index;
			while (true) {
				int max = Integer.MAX_VALUE;
				int mi = -1;
				int max2 = Integer.MAX_VALUE;
				int mj = -1;
				for (int i = 0; i < line; i++) {
					if (arr[i][1] == 0 && max > arr[i][0]) {
						max = arr[i][0];
						mi = i;
					}
				}
				for (int i = 0; i < line; i++) {
					if (i != mi && arr[i][1] == 0 && max2 > arr[i][0]) {
						max2 = arr[i][0];
						mj = i;
					}
				}
				if (mj == -1) {
					break;
				}
				arr[line++][0] = max + max2;
				arr[mi][1] = line - 1;
				arr[mj][1] = line - 1;

			}

			int max = 0;
			for (int i = 0; i < index; i++) {
				int j = arr[i][1];
				int a = 1;
				while (arr[j][1] != 0) {
					j = arr[j][1];
					a++;
				}
				max += a * arr[i][0];
			}
			System.out.println(max);
		}
		sc.close();

	}
}